home *** CD-ROM | disk | FTP | other *** search
/ Magnum One / Magnum One (Mid-American Digital) (Disc Manufacturing).iso / d12 / cbibcode.arc / SETFTIME.C < prev    next >
Encoding:
C/C++ Source or Header  |  1991-08-05  |  1.7 KB  |  56 lines

  1. /* SETFTIME.C --- p. 665 */
  2. #include <stdio.h>
  3. #include <fcntl.h>
  4. #include <io.h>
  5. main()
  6. {
  7.     char fname[40], *p_fname;
  8.     int filehandle;
  9.     unsigned date, time, day, month, year, hour, minute, second;
  10.     struct ftime dtinfo;
  11.     printf("Enter name of an existing file: ");
  12.     p_fname = gets(fname);
  13.                     /* Open the file using _open */
  14.     if((filehandle = _open(p_fname, O_RDONLY)) == -1)
  15.     {
  16.         printf("Error opening file: %s\n", fname);
  17.         exit(0);
  18.     }
  19.     printf("File %s opened.\n", fname);
  20.                     /*Ask for new date and time stamp */
  21.     printf("Enter new date in the format MM-DD-YY:");
  22.     scanf("%u-%u-%u", &month, &day, &year);
  23.     printf("Enter new time in the format HH:MM:SS ");
  24.     scanf("%u:%u:%u", &hour, &minute, &second);
  25.         /* Pack date and time information into single words */
  26.     dtinfo.ft_tsec = second/2;
  27.     dtinfo.ft_min = minute;
  28.     dtinfo.ft_hour = hour;
  29.     dtinfo.ft_day = day;
  30.     dtinfo.ft_month = month;
  31.                     /* NOTE : Year is relative to 1980
  32.                      * So we are subtracting 80. */
  33.     dtinfo.ft_year = year - 80;
  34.                     /* Set the date and time stamp */
  35.     setftime(filehandle, &dtinfo);
  36.             /* Get file's date and time stamp to verify the new
  37.              * date and time */
  38.     getftime(filehandle, &dtinfo);
  39.             /* Now extract the time and date infomation */
  40.     second = 2 * dtinfo.ft_tsec;
  41.     minute = dtinfo.ft_min;
  42.     hour = dtinfo.ft_hour;
  43.     day = dtinfo.ft_day;
  44.     month = dtinfo.ft_month;
  45.         /* NOTE: year is reletive to 1980. So we are adding 80 */
  46.     year = dtinfo.ft_year + 80;
  47.     printf("File: %s  Date: %d-%d-%d  Time: %.2d:%.2d:%.2d\n",
  48.             fname, month, day, year, hour, minute, second);
  49.                         /* Now close the file */
  50.     if(_close(filehandle) != 0)
  51.     {
  52.         printf("Error closing file with _close\n");
  53.         exit(0);
  54.     }
  55.     printf("File %s closed.\n", fname);
  56. }